/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lapi.dao;

import com.lapi.model.P100;
import com.lapi.util.HibernateUtil;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author HP
 */
public class P100Dao {

    private static final Log log = LogFactory.getLog(P100Dao.class);

    public List<P100> getListAll() {
        try {
            Object idf = HibernateUtil.createSession();
            Session session = HibernateUtil.getSession();
            Transaction tx = session.beginTransaction();
            Query query = session.createQuery("FROM P100");
            List<P100> list = query.list();
            tx.commit();
            HibernateUtil.closeSession(idf);
            return list;
        } catch (Exception ex) {
            Logger.getLogger(P100Dao.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    public P100 getP100(int pn01) {

        if (pn01 > 0) {
            try {
                Object idf = HibernateUtil.createSession();
                Session session = HibernateUtil.getSession();
                Transaction tx = session.beginTransaction();
                P100 p100 = (P100) session.get(P100.class, pn01);
                tx.commit();
                HibernateUtil.closeSession(idf);
                return p100;

            } catch (HibernateException ex) {
            } catch (Exception ex2) {
            }
        }
        return null;


    }

    public void updateP100(P100 p100) {
        if (p100.getPn01() > 0) {
            try {
                Object idf = HibernateUtil.createSession();
                Session session = HibernateUtil.getSession();
                Transaction tx = session.beginTransaction();
                session.saveOrUpdate(p100);
                tx.commit();
                HibernateUtil.closeSession(idf);

            } catch (HibernateException ex) {
                log.error("[updateP100] ", ex);
            } catch (Exception ex2) {
                log.error("[updateP100] ", ex2);
            }


        } else {
            log.error("[updateP100] " + "Error: No P100 given");
        }
    }

    public int addP100(P100 p100) {
        try {
            Object idf = HibernateUtil.createSession();
            Session session = HibernateUtil.getSession();
            Transaction tx = session.beginTransaction();
            Integer pn01 = (Integer) session.save(p100);
            tx.commit();
            HibernateUtil.closeSession(idf);
            return pn01;
        } catch (HibernateException ex) {
            log.error("[registerP100]", ex);
        } catch (Exception ex2) {
            log.error("[registerP100]", ex2);
        }
        return 0;
    }
}
